home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 80x0393.zip / SB-PROG.TXT < prev    next >
Text File  |  1993-03-30  |  22KB  |  477 lines

  1.  
  2.                       Programming the AdLib/Sound Blaster
  3.                                 FM Music Chips
  4.                            Version 2.0 (24 Feb 1992)
  5.  
  6.                   Copyright (c) 1991, 1992 by Jeffrey S. Lee
  7.  
  8.                                jlee@smylex.uucp
  9.  
  10.  
  11.  
  12.                         Warranty and Copyright Policy
  13.  
  14.      This document is provided on an "as-is" basis, and its author makes
  15.      no warranty or representation, express or implied, with respect to
  16.      its quality performance or fitness for a particular purpose.  In no
  17.      event will the author of this document be liable for direct, indirect,
  18.      special, incidental, or consequential damages arising out of the use
  19.      or inability to use the information contained within.  Use of this
  20.      document is at your own risk.
  21.  
  22.      This file may be used and copied freely so long as the applicable
  23.      copyright notices are retained, and no modifications are made to the
  24.      text of the document.  No money shall be charged for its distribution
  25.      beyond reasonable shipping, handling and duplication costs, nor shall
  26.      proprietary changes be made to this document so that it cannot be
  27.      distributed freely.  This document may not be included in published
  28.      material or commercial packages without the written consent of its
  29.      author.
  30.  
  31.  
  32.  
  33.                                    Overview
  34.  
  35.      Two of the most popular sound cards for the IBM-PC, the AdLib and the
  36.      Sound Blaster, suffer from a real dearth of clear documentation for
  37.      programmers.  AdLib Inc. and Creative Labs, Inc. both sell developers'
  38.      kits for their sound cards, but these are expensive, and (in the case
  39.      of the Sound Blaster developers' kit) can be extremely cryptic.
  40.  
  41.      This document is intended to provide programmers with a FREE source
  42.      of information about the programming of these sound cards.
  43.  
  44.      The information contained in this document is a combination of
  45.      information found in the Sound Blaster Software Developer's Kit, and
  46.      that learned by painful experience.  Some of the information may not
  47.      be valid for AdLib cards; if this is so, I apologize in advance.
  48.  
  49.      Please note that numbers will be given in hexadecimal, unless otherwise
  50.      indicated.  If a number is written out longhand (sixteen instead of 16)
  51.      it is in decimal.
  52.  
  53.  |   Changes from Version 1 of the file will be indicated by the use of change
  54.  |   bars in the left-hand margin.
  55.  
  56.  
  57.  
  58.                          Chapter One - Sound Card I/O
  59.  
  60.      The sound card is programmed by sending data to its internal registers
  61.      via its two I/O ports:
  62.  
  63.              0388 (hex) - Address/Status port  (R/W)
  64.              0389 (hex) - Data port            (W/O)
  65.  
  66.  |   The Sound Blaster Pro is capable of stereo FM music, which is accessed
  67.  |   in exactly the same manner.  Ports 0220 and 0221 (hex) are the address/
  68.  |   data ports for the left speaker, and ports 0222 and 0223 (hex) are the
  69.  |   ports for the right speaker.  Ports 0388 and 0389 (hex) will cause both
  70.  |   speakers to output sound.
  71.  
  72.      The sound card possesses an array of two hundred forty-four registers;
  73.      to write to a particular register, send the register number (01-F5) to
  74.      the address port, and the desired value to the data port.
  75.  
  76.      After writing to the register port, you must wait twelve cycles before
  77.      sending the data; after writing the data, eighty-four cycles must elapse
  78.      before any other sound card operation may be performed.
  79.  
  80.  |   The AdLib manual gives the wait times in microseconds: three point three
  81.  |   (3.3) microseconds for the address, and twenty-three (23) microseconds
  82.  |   for the data.
  83.  |
  84.  |   The most accurate method of producing the delay is to read the register
  85.  |   port six times after writing to the register port, and read the register
  86.  |   port thirty-five times after writing to the data port.
  87.  
  88.      The sound card registers are write-only.
  89.  
  90.      The address port also functions as a sound card status byte.  To
  91.      retrieve the sound card's status, simply read port 388.  The status
  92.      byte has the following structure:
  93.  
  94.               7      6      5      4      3      2      1      0
  95.           +------+------+------+------+------+------+------+------+
  96.           | both | tmr  | tmr  |              unused              |
  97.           | tmrs |  1   |  2   |                                  |
  98.           +------+------+------+------+------+------+------+------+
  99.  
  100.           Bit 7 - set if either timer has expired.
  101.               6 - set if timer 1 has expired.
  102.               5 - set if timer 2 has expired.
  103.  
  104.                        Chapter Two - The Registers
  105.  
  106. The following table shows the function of each register in the sound
  107. card.  Registers will be explained in detail after the table.  Registers
  108. not listed are unused.
  109.  
  110.    Address      Function
  111.    -------      ----------------------------------------------------
  112.      01         Test LSI / Enable waveform control
  113.      02         Timer 1 data
  114.      03         Timer 2 data
  115.      04         Timer control flags
  116.      08         Speech synthesis mode / Keyboard split note select
  117.    20..35       Amp Mod / Vibrato / EG type / Key Scaling / Multiple
  118.    40..55       Key scaling level / Operator output level
  119.    60..75       Attack Rate / Decay Rate
  120.    80..95       Sustain Level / Release Rate
  121.    A0..A8       Frequency (low 8 bits)
  122.    B0..B8       Key On / Octave / Frequency (high 2 bits)
  123.      BD         AM depth / Vibrato depth / Rhythm control
  124.    C0..C8       Feedback strength / Connection type
  125.    E0..F5       Wave Select
  126.  
  127. The groupings of twenty-two registers (20-35, 40-55, etc.) have an odd
  128. order due to the use of two operators for each FM voice.  The following
  129. table shows the offsets within each group of registers for each operator.
  130.  
  131.  
  132.    Channel        1   2   3   4   5   6   7   8   9
  133.    Operator 1    00  01  02  08  09  0A  10  11  12
  134.    Operator 2    03  04  05  0B  0C  0D  13  14  15
  135.  
  136. Thus, the addresses of the attack/decay bytes for channel 3 are 62 for
  137. the first operator, and 65 for the second.  (The address of the second
  138. operator is always the address of the first operator plus three).
  139.  
  140. To further illustrate the relationship, the addresses needed to control
  141. channel 5 are:
  142.  
  143.     29 - Operator 1  AM/VIB/EG/KSR/Multiplier
  144.     2C - Operator 2  AM/VIB/EG/KSR/Multiplier
  145.     49 - Operator 1  KSL/Output Level
  146.     4C - Operator 2  KSL/Output Level
  147.     69 - Operator 1  Attack/Decay
  148.     6C - Operator 2  Attack/Decay
  149.     89 - Operator 1  Sustain/Release
  150.     8C - Operator 2  Sustain/Release
  151.     A4 -             Frequency (low 8 bits)
  152.     B4 -             Key On/Octave/Frequency (high 2 bits)
  153.     C4 -             Feedback/Connection Type
  154.     E9 - Operator 1  Waveform
  155.     EC - Operator 2  Waveform
  156.  
  157.  
  158.  
  159.                        Explanations of Registers
  160.  
  161. Byte 01 - This byte is normally used to test the LSI device.  All bits
  162.           should normally be zero.  Bit 5, if enabled, allows the FM
  163.           chips to control the waveform of each operator.
  164.  
  165.              7     6     5     4     3     2     1     0
  166.           +-----+-----+-----+-----+-----+-----+-----+-----+
  167.           |   unused  | WS  |            unused           |
  168.           +-----+-----+-----+-----+-----+-----+-----+-----+
  169.  
  170.  
  171. Byte 02 - Timer 1 Data.  If Timer 1 is enabled, the value in this
  172.           register will be incremented until it overflows.  Upon
  173.           overflow, the sound card will signal a TIMER interrupt
  174.           (INT 08) and set bits 7 and 6 in its status byte.  The
  175.           value for this timer is incremented every eighty (80)
  176.           microseconds.
  177.  
  178.  
  179. Byte 03 - Timer 2 Data.  If Timer 2 is enabled, the value in this
  180.           register will be incremented until it overflows.  Upon
  181.           overflow, the sound card will signal a TIMER interrupt
  182.           (INT 08) and set bits 7 and 5 in its status byte.  The
  183.           value for this timer is incremented every three hundred
  184.           twenty (320) microseconds.
  185.  
  186.  
  187. Byte 04 - Timer Control Byte
  188.  
  189.         7     6     5     4     3     2     1     0
  190.      +-----+-----+-----+-----+-----+-----+-----+-----+
  191.      | IRQ | T1  | T2  |     unused      | T2  | T1  |
  192.      | RST | MSK | MSK |                 | CTL | CTL |
  193.      +-----+-----+-----+-----+-----+-----+-----+-----+
  194.  
  195.           bit 7 - Resets the flags for timers 1 & 2.  If set,
  196.                   all other bits are ignored.
  197.           bit 6 - Masks Timer 1.  If set, bit 0 is ignored.
  198.           bit 5 - Masks Timer 2.  If set, bit 1 is ignored.
  199.           bit 1 - When clear, Timer 2 does not operate.
  200.                   When set, the value from byte 03 is loaded into
  201.                   Timer 2, and incrementation begins.
  202.           bit 0 - When clear, Timer 1 does not operate.
  203.                   When set, the value from byte 02 is loaded into
  204.                   Timer 1, and incrementation begins.
  205.  
  206.  
  207. Byte 08 - CSM Mode / Keyboard Split.
  208.  
  209.         7     6     5     4     3     2     1     0
  210.      +-----+-----+-----+-----+-----+-----+-----+-----+
  211.      | CSM | Key |              unused               |
  212.      | sel | Spl |                                   |
  213.      +-----+-----+-----+-----+-----+-----+-----+-----+
  214.  
  215.           bit 7 - When set, selects composite sine-wave speech synthesis
  216.                   mode (all KEY-ON bits must be clear).  When clear,
  217.                   selects FM music mode.
  218.  
  219.           bit 6 - Selects the keyboard split point (in conjunction with
  220.                   the F-Number data).  The documentation in the Sound
  221.                   Blaster manual is utterly incomprehensible on this;
  222.                   I can't reproduce it without violating their copyright.
  223.  
  224.  
  225. Bytes 20-35 - Amplitude Modulation / Vibrato / Envelope Generator Type /
  226.               Keyboard Scaling Rate / Modulator Frequency Multiple
  227.  
  228.         7     6     5     4     3     2     1     0
  229.      +-----+-----+-----+-----+-----+-----+-----+-----+
  230.      | Amp | Vib | EG  | KSR |  Modulator Frequency  |
  231.      | Mod |     | Typ |     |       Multiple        |
  232.      +-----+-----+-----+-----+-----+-----+-----+-----+
  233.  
  234.           bit 7 - Apply amplitude modulation when set; AM depth is
  235.                   controlled by the AM-Depth flag in address BD.
  236.           bit 6 - Apply vibrato when set;  vibrato depth is controlled
  237.                   by the Vib-Depth flag in address BD.
  238.           bit 5 - When set, the sustain level of the voice is maintained
  239.                   until released; when clear, the sound begins to decay
  240.                   immediately after hitting the SUSTAIN phase.
  241.           bit 4 - Keyboard scaling rate.  This is another incomprehensible
  242.                   bit in the Sound Blaster manual.  From experience, if
  243.                   this bit is set, the sound's envelope is foreshortened as
  244.                   it rises in pitch.
  245.           bits 3-0 - These bits indicate which harmonic the operator will
  246.                   produce sound (or modulation) in relation to the voice's
  247.                   specified frequency:
  248.  
  249.                       0 - one octave below
  250.                       1 - at the voice's specified frequency
  251.                       2 - one octave above
  252.                       3 - an octave and a fifth above
  253.                       4 - two octaves above
  254.                       5 - two octaves and a major third above
  255.                       6 - two octaves and a fifth above
  256.                       7 - two octaves and a minor seventh above
  257.                       8 - three octaves above
  258.                       9 - three octaves and a major second above
  259.                       A - three octaves and a major third above
  260.                       B -  "       "     "  "   "     "     "
  261.                       C - three octaves and a fifth above
  262.                       D -   "      "     "  "   "     "
  263.                       E - three octaves and a major seventh above
  264.                       F -   "      "     "  "   "      "      "
  265.  
  266. Bytes 40-55 - Level Key Scaling / Total Level
  267.  
  268.         7     6     5     4     3     2     1     0
  269.      +-----+-----+-----+-----+-----+-----+-----+-----+
  270.      |  Scaling  |             Total Level           |
  271.      |   Level   | 24    12     6     3    1.5   .75 | <-- dB
  272.      +-----+-----+-----+-----+-----+-----+-----+-----+
  273.  
  274.           bits 7-6 - causes output levels to decrease as the frequency
  275.                      rises:
  276.  
  277.                           00   -  no change
  278.                           10   -  1.5 dB/8ve
  279.                           01   -  3 dB/8ve
  280.                           11   -  6 dB/8ve
  281.  
  282.           bits 5-0 - controls the total output level of the operator.
  283.                      all bits CLEAR is loudest; all bits SET is the
  284.                      softest.  Don't ask me why.
  285.  
  286.  
  287. Bytes 60-75 - Attack Rate / Decay Rate
  288.  
  289.         7     6     5     4     3     2     1     0
  290.      +-----+-----+-----+-----+-----+-----+-----+-----+
  291.      |         Attack        |          Decay        |
  292.      |          Rate         |          Rate         |
  293.      +-----+-----+-----+-----+-----+-----+-----+-----+
  294.  
  295.           bits 7-4 - Attack rate.  0 is the slowest, F is the fastest.
  296.           bits 3-0 - Decay rate.  0 is the slowest, F is the fastest.
  297.  
  298.  
  299. Bytes 80-95 - Sustain Level / Release Rate
  300.  
  301.         7     6     5     4     3     2     1     0
  302.      +-----+-----+-----+-----+-----+-----+-----+-----+
  303.      |     Sustain Level     |         Release       |
  304.      | 24    12     6     3  |          Rate         |
  305.      +-----+-----+-----+-----+-----+-----+-----+-----+
  306.  
  307.           bits 7-4 - Sustain Level.  0 is the loudest, F is the softest.
  308.           bits 3-0 - Release Rate.  0 is the slowest, F is the fastest.
  309.  
  310.  
  311. Bytes A0-B8 - Octave / F-Number / Key-On
  312.  
  313.         7     6     5     4     3     2     1     0
  314.      +-----+-----+-----+-----+-----+-----+-----+-----+
  315.      |        F-Number (least significant byte)      |  (A0-A8)
  316.      |                                               |
  317.      +-----+-----+-----+-----+-----+-----+-----+-----+
  318.  
  319.         7     6     5     4     3     2     1     0
  320.      +-----+-----+-----+-----+-----+-----+-----+-----+
  321.      |  Unused   | Key |    Octave       | F-Number  |  (B0-B8)
  322.      |           | On  |                 | most sig. |
  323.      +-----+-----+-----+-----+-----+-----+-----+-----+
  324.  
  325.           bit   5  - Channel is voiced when set, silent when clear.
  326.           bits 4-2 - Octave (0-7).  0 is lowest, 7 is highest.
  327.           bits 1-0 - Most significant bits of F-number.
  328.  
  329.      In octave 4, the F-number values for the chromatic scale and their
  330.      corresponding frequencies would be:
  331.  
  332.         F Number     Frequency     Note
  333.            16B          277.2       C#
  334.            181          293.7       D
  335.            198          311.1       D#
  336.            1B0          329.6       E
  337.            1CA          349.2       F
  338.            1E5          370.0       F#
  339.            202          392.0       G
  340.            220          415.3       G#
  341.            241          440.0       A
  342.            263          466.2       A#
  343.            287          493.9       B
  344.            2AE          523.3       C
  345.  
  346.  
  347. Bytes C0-C8 - Feedback / Algorithm
  348.  
  349.         7     6     5     4     3     2     1     0
  350.      +-----+-----+-----+-----+-----+-----+-----+-----+
  351.      |         unused        |    Feedback     | Alg |
  352.      |                       |                 |     |
  353.      +-----+-----+-----+-----+-----+-----+-----+-----+
  354.  
  355.           bits 3-1 - Feedback strength.  If all three bits are set to
  356.                      zero, no feedback is present.  With values 1-7,
  357.                      operator 1 will send a portion of its output back
  358.                      into itself.  1 is the least amount of feedback,
  359.                      7 is the most.
  360.           bit 0    - If set to 0, operator 1 modulates operator 2.  In this
  361.                      case, operator 2 is the only one producing sound.
  362.                      If set to 1, both operators produce sound directly.
  363.                      Complex sounds are more easily created if the algorithm
  364.                      is set to 0.
  365.  
  366.  
  367. Byte BD - Amplitude Modulation Depth / Vibrato Depth / Rhythm
  368.  
  369.         7     6     5     4     3     2     1     0
  370.      +-----+-----+-----+-----+-----+-----+-----+-----+
  371.      | AM  | Vib | Rhy | BD  | SD  | TOM | Top | HH  |
  372.      | Dep | Dep | Ena |     |     |     | Cym |     |
  373.      +-----+-----+-----+-----+-----+-----+-----+-----+
  374.  
  375.           bit 7 - Set:    AM depth is 4.8dB
  376.                   Clear:  AM depth is 1 dB
  377.           bit 6 - Set:    Vibrato depth is 14 cent
  378.                   Clear:  Vibrato depth is 7 cent
  379.           bit 5 - Set:    Rhythm enabled  (6 melodic voices)
  380.                   Clear:  Rhythm disabled (9 melodic voices)
  381.           bit 4 - Bass drum on/off
  382.           bit 3 - Snare drum on/off
  383.           bit 2 - Tom tom on/off
  384.           bit 1 - Cymbal on/off
  385.           bit 0 - Hi Hat on/off
  386.  
  387.           Note:  KEY-ON registers for channels 06, 07, and 08 must be OFF
  388.                  in order to use the rhythm section.  Other parameters
  389.                  such as attack/decay/sustain/release must also be set
  390.                  appropriately.
  391.  
  392.  
  393. Bytes E0-F5 - Waveform Select
  394.  
  395.         7     6     5     4     3     2     1     0
  396.      +-----+-----+-----+-----+-----+-----+-----+-----+
  397.      |               unused              |  Waveform |
  398.      |                                   |  Select   |
  399.      +-----+-----+-----+-----+-----+-----+-----+-----+
  400.  
  401.           bits 1-0 - When bit 5 of address 01 is set, the output waveform
  402.                      will be distorted according to the waveform indicated
  403.                      by these two bits.  I'll try to diagram them here,
  404.                      but this medium is fairly restrictive.
  405.  
  406.          ___              ___            ___    ___       _      _
  407.         /   \            /   \          /   \  /   \     / |    / |
  408.        /_____\_______   /_____\_____   /_____\/_____\   /__|___/__|___
  409.               \     /
  410.                \___/
  411.  
  412.             00              01               10               11
  413.  
  414.  
  415.  
  416.  |                          Detecting a Sound Card
  417.  |
  418.  |   According to the AdLib manual, the 'official' method of checking for a
  419.  |   sound card is as follows:
  420.  |
  421.  |      1)  Reset both timers by writing 60h to register 4.
  422.  |      2)  Enable the interrupts by writing 80h to register 4.  NOTE: this
  423.  |          must be a separate step from number 1.
  424.  |      3)  Read the status register (port 388h).  Store the result.
  425.  |      4)  Write FFh to register 2 (Timer 1).
  426.  |      5)  Start timer 1 by writing 21h to register 4.
  427.  |      6)  Delay for at least 80 microseconds.
  428.  |      7)  Read the status register (port 388h).  Store the result.
  429.  |      8)  Reset both timers and interrupts (see steps 1 and 2).
  430.  |      9)  Test the stored results of steps 3 and 7 by ANDing them
  431.  |          with E0h.  The result of step 3 should be 00h, and the
  432.  |          result of step 7 should be C0h.  If both are correct, an
  433.  |          AdLib-compatible board is installed in the computer.
  434.  |
  435.  |
  436.  |                              Making a Sound
  437.  |
  438.  |   Many people have asked me, upon reading this document, what the proper
  439.  |   register values should be to make a simple sound.  Well, here they are.
  440.  |
  441.  |   First, clear out all of the registers by setting all of them to zero.
  442.  |   This is the quick-and-dirty method of resetting the sound card, but it
  443.  |   works.  Note that if you wish to use different waveforms, you must then
  444.  |   turn on bit 5 of register 1.  (This reset need be done only once, at the
  445.  |   start of the program, and optionally when the program exits, just to
  446.  |   make sure that your program doesn't leave any notes on when it exits.)
  447.  |
  448.  |   Now, set the following registers to the indicated value:
  449.  |
  450.  |     REGISTER     VALUE     DESCRIPTION
  451.  |        20          01      Set the modulator's multiple to 1
  452.  |        40          10      Set the modulator's level to about 40 dB
  453.  |        60          F0      Modulator attack:  quick;   decay:   long
  454.  |        80          77      Modulator sustain: medium;  release: medium
  455.  |        A0          98      Set voice frequency's LSB (it'll be a D#)
  456.  |        23          01      Set the carrier's multiple to 1
  457.  |        43          00      Set the carrier to maximum volume (about 47 dB)
  458.  |        63          F0      Carrier attack:  quick;   decay:   long
  459.  |        83          77      Carrier sustain: medium;  release: medium
  460.  |        B0          31      Turn the voice on; set the octave and freq MSB
  461.  |
  462.  |   To turn the voice off, set register B0h to 11h (or, in fact, any value
  463.  |   which leaves bit 5 clear).  It's generally preferable, of course, to
  464.  |   induce a delay before doing so.
  465.  |
  466.  |
  467.  |                             Acknowledgements
  468.  |
  469.  |   Thanks are due to the following people:
  470.  |
  471.  |   Ezra M. Dreisbach (ed10+@andrew.cmu.edu), for providing the information
  472.  |     about the recommended port write delay from the AdLib manual, and the
  473.  |     'official' method of detecting an AdLib-compatible sound card.
  474.  |
  475.  |   Nathan Isaac Laredo (gt7080a@prism.gatech.edu), for providing the
  476.  |     port numbers for stereo sound on the Sound Blaster Pro.
  477.